#############################################################
# Dynamic DiD with Covariates: Visas Haiti and Crime Rates
#############################################################

rm(list=ls())

library(Hmisc)
library(did)
library(readstata13)
library(foreign)
library(tidyverse)
library(ggplot2)
set.seed(1111)

#sink("09_results_rates_haiti_covariates.txt")

############################
# Read and prepare data 
############################

# load data
load("final_county_2023april6.RData")
names(d_county)

#######################
# Results 
#######################

# d-did
out8 <- att_gt(yname = "crime_rates_s",
               gname = "first.treat_haiti",
               idname = "county",
               tname = "survey",
               xformla = ~turnout_2013 + income_2003 + population_2011 + right_voteshare_2013,
               data = d_county,
               est_method = "reg"
)

es8 <- aggte(out8, type = "dynamic",na.rm = TRUE)
summary(es8)
ggdid(es8)

# save results for overall effect
pe8 <- es8$overall.att
se8 <- es8$overall.se
results8 <- rbind(pe8,se8) 
results8
write.table(results8, "overallATT_8.txt", sep="\t")

# table a21
sink(file = "tableA21.txt")
summary(es8)
sink(file = NULL)

sink()
